<?php
/**
 * DynaGrid - The PHP datagrid
 *
 * @package    DynaGrid
 * @author     $Author: keesschepers $
 * @version    $Id: Grid.php 11 2011-02-11 23:20:17Z keesschepers $
 */

include_once 'DataSource/Adapter/Abstract.php';

/**
 * The Array plugin makes it possible to pass thru a numeric indexed array containing fieldname => value.
 *
 */
class DynaGrid_DataSource_Adapter_Array extends DynaGrid_DataSource_Adapter_Abstract implements DynaGrid_DataSource_Adapter_Interface {
	const ARRAY_DATA = 0;
	/**
	 *
	 * The internal pointer where the array needs to start.
	 *
	 * @var integer
	 */
	private $offset = 0;
	/**
	 * Limiting of the result is not done immediatly because otherwise totalrows
	 * count isn't correct. For this reason we save this value internal and the self::getData
	 * function limits the result.
	 * 
	 * @var integer 
	 */
	private $limit = 0;
	private $totalRows = 0;
  public function getTotalRows() {
		return $this->totalRows;
	}
	/**
	 * Done a little tricky but it works..
	 *
	 * @param integer $offset
	 */
	public function setOffset($offset) {
		$this->offset = $offset;
	}
	/**
	 *
	 * Pops elements of the data array until the limit needed number is reached.
	 *
	 * @param integer $limit
	 */
	public function setLimit($limit) {
		$this->limit = $limit;
	}
	/**
	 * Is it nasty? Altough it works!
	 *
	 * @return array
	 */
	public function getFields() {
		return (array)array_keys($this->arguments[self::ARRAY_DATA][0]);
	}
	/**
	 * Return the tabel data. Make a copy of the internal data because
	 * otherwise totalrows, etc won't work.
	 *
	 * @return array
	 */
	public function  getData() {
		$this->totalRows = count($this->arguments[self::ARRAY_DATA]);

		$data = $this->arguments[self::ARRAY_DATA];
		
		/**
		 * According to the offset shift elements from the beginning of the array to
		 * accomplish the result the user wants.
		 */
		for($i=0; $i < $this->offset; $i++) array_shift($data);

		$amountRemove = $this->totalRows - ($this->offset + $this->limit);
		
		for($i=0; $i < $amountRemove; $i++) array_pop($data);

		return $data;
	}
	
	/**
	 *
	 * This function support sorting on multiple fields.
	 * @todo make a difference between descending and ascending
	 * @param array $properties Array of properties to sort on
	 */
	public function setOrderBy(array $properties) {
		$data = $this->arguments[self::ARRAY_DATA];
		
		foreach($properties as $sorting) {
			
			if(!isset($sorting['field']) || !isset($sorting['direction'])) {
				throw new DynaGrid_Exception('Unable to sort data when there is no field or direction given.');
			}
			
			uasort($data, function($first, $second) use ($sorting) {			
				switch(strtolower($sorting['direction'])) {
					case 'asc' : 
					default :
						return strcmp($first[$sorting['field']], $second[$sorting['field']]);
						break;
					case 'desc' :
						return strcmp($second[$sorting['field']], $first[$sorting['field']]);
						break;						
				}
				
			});	
		}
		
		$this->arguments[self::ARRAY_DATA] = $data;
	}
}
?>
